﻿using B.S.DataBase.MyProject.Domain.Vendors;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.VendorsCommand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.VendorsCommandHandler
{
    /// <summary>
    /// 选择供应商
    /// </summary>
    public class GetMaterCommdHander : IRequestHandler<GetMaterCommd, APIResult<List<MaterialSupplier>>>
    {

        private readonly IBaseRepository<MaterialSupplier> repository;

        public GetMaterCommdHander(IBaseRepository<MaterialSupplier> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<MaterialSupplier>>> Handle(GetMaterCommd request, CancellationToken cancellationToken)
        {
            APIResult<List<MaterialSupplier>> result  =new APIResult<List<MaterialSupplier>>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();

            var list = repository.GetAll().Result;

            if(!string.IsNullOrEmpty(request.SupplierName))
            {
                list =list.Where(X=>X.SupplierName.Contains(request.SupplierName));
            }
            if (!string.IsNullOrEmpty(request.Bines))
            {
                list = list.Where(X => X.Bines.Contains(request.Bines));
            }


            result.data =list.ToList();

            return Task.FromResult(result);
        }
    }
}
